package com.wuliu.action.car;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONObject;

import beartool.MD5;

import com.wuliu.action.BaseAction;
import com.wuliu.business.car.CarObj;
import com.wuliu.business.sys.UserObj;
import com.wuliu.model.common.PageVO;
import com.wuliu.model.common.QueryVO;
import com.wuliu.model.car.Car;
import com.wuliu.model.sys.User;
import com.wuliu.util.BaseUtil;
import com.wuliu.util.DbUtil;

public class CarAction extends BaseAction {
	private Car car;
	
    public Car getCar() {
		return car;
	}

	public void setCar(Car car) {
		this.car = car;
	}

	public String carList() {  
		List<QueryVO> list = new ArrayList<QueryVO>(); 
        QueryVO CAR_ID = new QueryVO("A.CAR_ID", "选择", "checkbox", "chioce", "");
        list.add(CAR_ID); 
        
        QueryVO  CAR_CODE = new QueryVO("A.CAR_CODE", "配送点编码", "1");
        list.add(CAR_CODE);
        
        QueryVO  CAR_NAME = new QueryVO("A.CAR_NAME", "配送点名称", "1");
        list.add(CAR_NAME);
        
        QueryVO  PROVINCE = new QueryVO("A.PROVINCE", "省", "1");
        list.add(PROVINCE);
        
        QueryVO  CITY = new QueryVO("A.CITY", "市", "1");
        list.add(CITY);
        
        QueryVO  TOWN = new QueryVO("A.TOWN", "县", "1");
        list.add(TOWN);
        
        QueryVO  CAR_ADDR = new QueryVO("A.CAR_ADDR", "详细地址", "1");
        list.add(CAR_ADDR);
        
        QueryVO CAR_TEL = new QueryVO("A.CAR_TEL", "配送点电话", "1");
        list.add(CAR_TEL);
        
        QueryVO ACTIVE_FLAG = new QueryVO("(CASE WHEN A.ACTIVE_FLAG='0' THEN '可用' ELSE '不可用' END) AS ACTIVE_FLAG ", "是否可用", "1");
        list.add(ACTIVE_FLAG);
        
        QueryVO ADD_BY = new QueryVO("A.ADD_BY", "创建人", "1");
        list.add(ADD_BY);  
        
        QueryVO ADD_DATE = new QueryVO("A.ADD_DATE", "创建日期", "1");
        list.add(ADD_DATE);
        
        QueryVO ADD_TIME = new QueryVO("A.ADD_TIME", "创建时间", "1");
        list.add(ADD_TIME);
        
        QueryVO UPD_BY = new QueryVO("A.UPD_BY", "最后修改人", "1");
        list.add(UPD_BY);  
        
        QueryVO UPD_DATE = new QueryVO("A.UPD_DATE", "最后修改日期", "1");
        list.add(UPD_DATE);
        
        QueryVO UPD_TIME = new QueryVO("A.UPD_TIME", "最后修改时间", "1");
        list.add(UPD_TIME);
        

        String intStr = "1000";// 需要特殊显示的标记;在原来基础上加1000 
        String tableName = "  WL_CAR A WHERE 1=1 ";  
 	    
        String orderStr = " ORDER BY A.CAR_NAME ASC "; 
       
        HashMap<String, String> hm = null; //DBHelper.formQueryToken(request);
        HashMap<String, String> hmSearch = new HashMap<String, String>();
        String searchFlag = BaseUtil.getStr(request.getParameter("searchFlag"));
        String s_role_name = BaseUtil.getStr(request.getParameter("s_role_name"));
        String s_role_desc = BaseUtil.getStr(request.getParameter("s_role_desc"));  
        
    	 if(!s_role_name.equals("")){
           	tableName += " AND A.ROLE_NAME LIKE '%"+s_role_name+"%' "; 
           	hmSearch.put("s_role_name", s_role_name);
         }
    	 if(!s_role_desc.equals("")){
           	tableName += " AND A.ROLE_DESC LIKE '%"+s_role_desc+"%'";
           	hmSearch.put("s_role_desc", s_role_desc);
         } 
    	 
        String pageNum = BaseUtil.getStr(request.getParameter("pageNums"));
        if(pageNum==null||pageNum.equals(""))
            pageNum = "1"; 
        List<?> resultL = DbUtil.queryStrForPage(list, tableName, intStr, hm, orderStr, Integer.parseInt(pageNum));
        int queryCount = DbUtil.queryStrCount(list, tableName, intStr, hm, orderStr);
        PageVO vo = DbUtil.pageObjFor(resultL, pageNum, queryCount);  
        
        request.setAttribute("hmSearch", hmSearch);
        request.setAttribute("searchFlag", searchFlag); 
         
        request.setAttribute("voResult", vo);
        request.setAttribute("qList", list);// 显示名称 
        return "carList";
	}
	
    public String saveUser(){

    	if(BaseUtil.getStr(car.getCarId()).equals("")){
        	if(BaseUtil.getStr(CarObj.getCarByCode(car.getCarCode()).getCarCode()).equals("")){
        		String sql	= "INSERT INTO WL_CAR(CAR_ID,CAR_CODE,WORK_CODE,CAR_TYPE,CAR_SIZE," +
        				"CAR_TON,BUY_DATE,ACTIVE_FLAG,ADD_DATE,ADD_TIME,ADD_BY) " +
        				" VALUES(?,?,?,?,?,?,?,?,?,DATE_FORMAT(NOW(),'%Y%m%d') ,DATE_FORMAT(NOW(),'%H%i%s'),?)";
        		List<String> lt = new ArrayList<String>();
        		lt.add(car.getCarCode());
        		lt.add(car.getCarCode());
        		lt.add(car.getWorkCode());
        		lt.add(car.getCarType());
        		lt.add(car.getCarSize().toString());
        		lt.add(car.getCarTon().toString());
        		lt.add(car.getBuyDate());
        		lt.add(car.getActiveFlag());
        		lt.add(((User)session.get("user")).getUserName());
        		boolean result	= DbUtil.excuSQL(lt, sql);
        		if(result){
        			request.setAttribute("showMsg","新增成功！");
        		}else{
        			request.setAttribute("showMsg","新增失败！");
        		} 				
        	}else{
        		request.setAttribute("showMsg","该编号已存在！");
        	}
    	}else{
    		String sql	= "UPDATE WL_CAR SET CAR_CODE=?,WORK_CODE=?,CAR_TYPE=?,CAR_SIZE=?," +
    				"CAR_TON=?,BUY_DATE=?,ACTIVE_FLAG=?,UPD_DATE=DATE_FORMAT(NOW(),'%Y%m%d')," +
    				"UPD_TIME=DATE_FORMAT(NOW(),'%H%i%s'),UPD_BY=? WHERE CAR_CODE=? ";
    		List<String> lt = new ArrayList<String>();
    		
    		lt.add(car.getCarCode());
    		lt.add(car.getWorkCode());
    		lt.add(car.getCarType());
    		lt.add(car.getCarSize().toString());
    		lt.add(car.getCarTon().toString());
    		lt.add(car.getBuyDate());
    		lt.add(car.getActiveFlag());
    		lt.add(((User)session.get("user")).getUserName());
    		lt.add(car.getCarCode());
    		boolean result	= DbUtil.excuSQL(lt, sql);
    		if(result){
    			request.setAttribute("showMsg","更新成功！");
    		}else{
    			request.setAttribute("showMsg","更新失败！");
    		} 				
    	}
    	return carList();
    }
    
    public String getCarInfo(){
    	String id	= BaseUtil.getStr(request.getParameter("id"));
    	Car car = CarObj.getCarById(id);

    	JSONObject json = JSONObject.fromObject(car);
    	try {
			response.setCharacterEncoding("utf8");
			response.getWriter().print(json.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return null;
    }
    
	 public String delCar(){
		 String id	= request.getParameter("id");

		 String sql	= "DELETE WL_CAR WHERE CAR_ID ='"+id+"'";

		 boolean result	= DbUtil.execute(sql);
		 if(result){
			 request.setAttribute("showMsg","删除成功！");
		 }else{
			 request.setAttribute("showMsg", "删除失败！");
		 }
		 return carList();
	 }
}
